﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using GioHangViet.Areas.Admin.Models;
using GioHangViet.Models;

namespace GioHangViet.Areas.Admin.Controllers
{
    public class CategoryController : Controller
    {
        private readonly GoLungTungModel _db = new GoLungTungModel();

        //
        // GET: /Admin/Category/

        public ViewResult Index(int id)
        {
            if (id < 1)
            {
                id = 1;
            }
            ViewBag.CurrentPage = id;
            ViewBag.countRecord = _db.Categories.Count();
            var pageSize = 10D;
            if (Double.TryParse(Resources.Constant.PageSize, out pageSize))
            {
                ViewBag.PageSize = pageSize;
            }
            else
            {
                ViewBag.PageSize = pageSize;
            }
            var model =
                _db.Categories.ToList().Skip((id - 1)*Convert.ToInt32(Resources.Constant.PageSize)).Take(
                    Convert.ToInt32(Resources.Constant.PageSize));
            return View(model);
        }
        //
        // GET: /Admin/Category/Create

        public ActionResult Create()
        {
            ViewBag.Categories = new SelectList(_db.Categories.ToList(), "Alias", "CategoryName");
            var category = new Category { Alias = "new", UpdateBy = "Admin" };
            return View(category);
        }

        //
        // POST: /Admin/Category/Create

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                var alias = new ProcessAlias();
                category.Alias = alias.CreateAlias(category.CategoryName, "Categories", "Name");
                _db.Categories.Add(category);
                _db.SaveChanges();
                return RedirectToAction("Index", new { id = 1 });
            }
            ViewBag.Categories = new SelectList(_db.Categories.ToList(), "Alias", "CategoryName");
            return View(category);
        }

        //
        // GET: /Admin/Category/Edit/5

        public ActionResult Edit(string id)
        {
            Category category = _db.Categories.Find(id);
            ViewBag.Categories = new SelectList(_db.Categories.ToList(), "Alias", "CategoryName");
            return View(category);
        }

        //
        // POST: /Admin/Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(category).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index", new { id = 1 });
            }
            ViewBag.Categories = new SelectList(_db.Categories.ToList(), "Alias", "CategoryName");
            return View(category);
        }

        //
        // GET: /Admin/Category/Delete/5

        public ActionResult Delete(string id)
        {
            var category = _db.Categories.Find(id);
            if (category != null)
            {
                if (_db.CategoryProducts.Any(product => product.CategoryAlias == id))
                {
                    ViewBag.ErrorMessage = Resources.Message.DeleteContrainCategory;
                    ViewBag.CurrentPage = 1;
                    ViewBag.countRecord = _db.Categories == null ? 0 : _db.Categories.Count();
                    var pageSize = 10D;
                    if (Double.TryParse(Resources.Constant.PageSize, out pageSize))
                    {
                        ViewBag.PageSize = pageSize;
                    }
                    else
                    {
                        ViewBag.PageSize = pageSize;
                    }
                    var model =
                        _db.Categories.ToList().Take(Convert.ToInt32(Resources.Constant.PageSize));
                    return View("Index",model);
                }
                _db.Categories.Remove(category);
                _db.SaveChanges();
                return RedirectToAction("Index", new { id = 1 });
            }
            return View("Error");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}